package com.example.myapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Spinner;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.db.DBManager;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;

import java.util.ArrayList;
import java.util.List;

public class pieChart_inActivity extends AppCompatActivity {

    //①实例化一个 List  用来保存你的数据，这里是pieEntry
    List<PieEntry> list=new ArrayList<>();

    int year1,month1,day;
    String in;
    float [] money=new float[6];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.chart_income);
        setTitle("PieChart_inActivity");

        PieChart pie = (PieChart) findViewById(R.id.pie);
        Spinner year_spinner = findViewById(R.id.spinner_month);
        //类型的选择
        Spinner month_spinner = findViewById(R.id.spinner_year);



        year_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                String result = adapterView.getItemAtPosition(position).toString();
                year1=Integer.parseInt(result.split("年")[0]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        month_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                String result = adapterView.getItemAtPosition(position).toString();
                month1=Integer.parseInt(result.toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        //②输入数据
        //其中两个数字对应的分别是 X轴Y轴


        money[0]= DBManager.getMoneyBytype(year1,month1,in,"工资");
        money[1]=DBManager.getMoneyBytype(year1,month1,in,"兼职");
        money[2]=DBManager.getMoneyBytype(year1,month1,in,"理财");
        money[3]=DBManager.getMoneyBytype(year1,month1,in,"礼金");
        money[4]=DBManager.getMoneyBytype(year1,month1,in,"其他");
        money[5]=DBManager.getMoneyBytype(year1,month1,in,"设置");


        float total=money[0]+money[1]+money[2]+money[3]+money[4]+money[5];

        list.add(new PieEntry(money[0]/total,"工资"));
        list.add(new PieEntry(money[1]/total, "兼职"));
        list.add(new PieEntry(money[2]/total, "理财"));
        list.add(new PieEntry(money[3]/total, "礼金"));
        list.add(new PieEntry(money[4]/total, "其他"));
        list.add(new PieEntry(money[5]/total, "设置"));



//③定义一下饼图的颜色
        ArrayList<Integer> colors = new ArrayList<Integer>();
        colors.add(Color.BLUE);
        colors.add(Color.CYAN);
        colors.add(Color.MAGENTA);
        colors.add(Color.GRAY);
        colors.add(Color.LTGRAY);


//⑤实例化Dataset
        //list是你这条线的数据  "销售比例" 是你对这个饼的描述
        PieDataSet PieDataSet = new PieDataSet(list, "该月各类型支出排行");
        PieDataSet.setSliceSpace(0f); //设置饼状Item之间的间隙
        PieDataSet.setColors(colors); //为DataSet中的数据匹配上颜色集(饼图Item颜色)
        PieData pieData = new PieData(PieDataSet);
        pieData.setDrawValues(true);            //设置是否显示数据实体(百分比，true:以下属性才有意义)
        pieData.setValueTextColor(Color.WHITE);  //设置所有DataSet内数据实体（百分比）的文本颜色
        pieData.setValueTextSize(12f);
        pieData.setValueFormatter(new PercentFormatter());

        pie.setHoleRadius(0);
        pie.setTransparentCircleRadius(0);
        pie.setData(pieData);
        pie.invalidate();
    }
}
